//package com.alibaba.cirtus.ots.application.app.ability.facade;
//
//import com.alibaba.cz.base.tool.log.Log;
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.alibaba.lattice2.epoch.sdk.model.Result;
//import com.epoch.app.bcots.model.dto.*;
//import com.epoch.app.bcots.service.BaseDataService;
//import com.epoch.app.bcots.service.PlatformEnhanceService;
//import com.epoch.app.bcots.service.UnifiedInventoryService;
//import com.epoch.app.otsapplicationappsdk.inventoryinput.dto.InitRequest;
//import com.epoch.app.otsapplicationappsdk.inventoryinput.dto.SubmitRequest;
//import com.epoch.app.otsapplicationappsdk.inventoryinput.service.InventoryInputService;
//import com.epoch.app.otsplatformenhance.goodsfavorites.dto.QueryGoodsFavoritesRequest;
//import com.epoch.app.otsplatformenhance.goodsfavorites.model.GoodsFavorites;
//import com.epoch.app.otsplatformenhance.goodsfavorites.service.GoodsFavoritesService;
//import com.epoch.app.otsunifiedinventory.model.dto.SubmitStoreInventoryOrderRequest;
//import com.epoch.app.otsunifiedinventory.service.OtsUnifiedInventoryService;
//import com.epoch.app.otsunifiedinventory.service.StoreStockService;
//import org.apache.commons.collections.CollectionUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.util.StopWatch;
//
//import javax.annotation.Resource;
//import java.util.*;
//
//@Service
//public class InventoryInputServiceImpl implements InventoryInputService {
//
//    private static Log log = Log.getLogger(InventoryInputServiceImpl.class);
//
//    @Autowired
//    private BaseDataService baseDataService;
//
//    @Autowired
//    private StoreStockService storeStockService;
//
//    @Autowired
//    private PlatformEnhanceService platformEnhanceService;
//
//    @Autowired
//    private UnifiedInventoryService unifiedInventoryService;
//
//    @Autowired
//    private OtsUnifiedInventoryService otsUnifiedInventoryService;
//
//    @Resource
//    private GoodsFavoritesService favoritesService;
//
//    @Override
//    public Object init(InitRequest initRequest) {
//        JSONObject result = new JSONObject();
//
//        List goodsFavoritesList = new ArrayList();
//
//        //当前用户收藏夹货品
//        try {
//            Result<List<GoodsFavorites>> listResult = favoritesService.queryGoodsFavorites(new QueryGoodsFavoritesRequest());
//            if (Objects.nonNull(listResult)&& CollectionUtils.isNotEmpty(listResult.getResult())){
//                GoodsFavorites goodsFavorites = listResult.getResult().get(0);
//                JSONArray goodsList = JSONArray.parseArray(goodsFavorites.getGoodsList());
//                if (CollectionUtils.isNotEmpty(goodsList)) {
//                    for (Object goodsObj : goodsList) {
//                        JSONObject goodsJson = (JSONObject) JSONObject.toJSON(goodsObj);
//                        goodsFavoritesList.add(goodsJson.getString("goodsCode"));
//                    }
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        try {
//            StopWatch stopWatch = new StopWatch();
//            stopWatch.start();
//
//            //前端透传常用货品code，下发到接口返回常用货品类目
//            QueryGoodsListRequest queryGoodsListRequest = new QueryGoodsListRequest();
//            queryGoodsListRequest.setRecentlyUsedItemIdList(initRequest.getGoodsIdList());
//            if (CollectionUtils.isNotEmpty(goodsFavoritesList)) {
//                queryGoodsListRequest.setGoodsFavoritesList(goodsFavoritesList);
//            }
//            Result<List<QueryGoodsListResponse>> queryGoodsListResponse = platformEnhanceService.queryGoodsList(queryGoodsListRequest);
//            stopWatch.stop();
//            log.info("queryGoodsList耗时"+stopWatch.getTotalTimeMillis());
//            //取默认门店
//            stopWatch.start();
//            WarehouseInfo warehouseInfo = getDefaultStoreInfo();
//            stopWatch.stop();
//            log.info("getDefaultStoreInfo耗时"+stopWatch.getTotalTimeMillis());
//            result.put("selectedId", warehouseInfo.getId());
//            result.put("selectedValue", warehouseInfo.getName());
//            result.put("DataResult", queryGoodsListResponse.getResult());
////            result.put("DataResult",buildGoodsList(queryGoodsListResponse.getResult(),initRequest.getGoodsIdList()));
//            result.put("success", true);
//        }catch (Exception e){
//            result.put("success", false);
//            result.put("msg",e.getMessage());
//        }
//        return result;
//    }
//
//    //取默认门店id
//    private WarehouseInfo getDefaultStoreInfo() throws Exception{
//        SearchStoreTerminalForPullRequest request = new SearchStoreTerminalForPullRequest();
//        request.setModel(6);
//        log.info("unifiedInventoryService.wmsQueryStoreTerminalList$request:" + JSON.toJSONString(request));
//        Result<List<WarehouseInfo>> response = unifiedInventoryService.searchStoreTerminalForPull(request);
//        log.info("unifiedInventoryService.wmsQueryStoreTerminalList$response:" + JSON.toJSONString(response));
//        if (response == null || CollectionUtils.isEmpty(response.getResult())) {
//            throw new Exception("未查到当前用户组织权限下的门店");
//        }
//        return response.getResult().get(0);
//    }
//
//    //构造常用货品类目
//    public List<QueryGoodsListResponse> buildGoodsList(List<QueryGoodsListResponse> queryGoodsList,List goodsIdList) {
//        if (org.apache.commons.collections.CollectionUtils.isEmpty(goodsIdList)) {
//            return queryGoodsList;
//        }
//        if (org.apache.commons.collections.CollectionUtils.isEmpty(queryGoodsList)) {
//            return queryGoodsList;
//        }
//        Set<String> goodsIdSet = new HashSet();
//        for (Object goodsId : goodsIdList) {
//            goodsIdSet.add(goodsId.toString());
//        }
//        List list = new ArrayList();
//        for (String goodsId : goodsIdSet) {
//            boolean flg = false;
//            for (QueryGoodsListResponse queryGoodsListResponse : queryGoodsList) {
//                flg = false;
//                if (org.apache.commons.collections.CollectionUtils.isNotEmpty(queryGoodsListResponse.getGoodsList())) {
//                    for (Object goodsObj : queryGoodsListResponse.getGoodsList()) {
//                        JSONObject goodsJson = (JSONObject) JSONObject.toJSON(goodsObj);
//                        if (goodsId.equals(goodsJson.getString("id"))) {
//                            list.add(goodsObj);
//                            flg = true;
//                            break;
//                        }
//                    }
//                }
//                if (flg) break;
//            }
//        }
//        if (CollectionUtils.isEmpty(list)) {
//            return queryGoodsList;
//        }
//        QueryGoodsListResponse queryGoodsListResponse = new QueryGoodsListResponse();
//        //端上构造的虚假常用货品类目,库中无该类目
//        queryGoodsListResponse.setLabel("常用货品");
//        queryGoodsListResponse.setGoodsList(list);
//
//        List<QueryGoodsListResponse> queryGoodsListResponseList = new ArrayList<>();
//        queryGoodsListResponseList.add(queryGoodsListResponse);
//        queryGoodsListResponseList.addAll(queryGoodsList);
//
//        return queryGoodsListResponseList;
//    }
//
//    @Override
//    public Object submit(SubmitRequest submitRequest){
//        JSONObject result = new JSONObject();
//        try {
//            JSONObject request = (JSONObject) JSONObject.toJSON(submitRequest.getAttr());
//            log.info("input_submit$request:" + JSON.toJSONString(request));
//            if (request != null && request.getString("fields") != null) {
//                JSONArray dataArray = request.getJSONObject("fields").getJSONArray("dataSource");
//                List dataList = buildDataList(dataArray);
//                String storeId =  request.getJSONObject("fields").getString("selectedId");
//                SubmitStoreInventoryOrderRequest submitStoreInventoryOrderRequest = new SubmitStoreInventoryOrderRequest();
//                submitStoreInventoryOrderRequest.setStoreId(storeId);
//                submitStoreInventoryOrderRequest.setRemark("");
//                submitStoreInventoryOrderRequest.setDetailList(dataList);
//                log.info("submitStoreInventoryOrder$request:" + JSON.toJSONString(submitStoreInventoryOrderRequest));
//                Object object = storeStockService.submitStoreInventoryOrder(submitStoreInventoryOrderRequest);
//                log.info("submitStoreInventoryOrder$response:" + JSON.toJSONString(object));
//                JSONObject jsonObject = (JSONObject) JSONObject.toJSON(object);
//                result.put("success", jsonObject.getBoolean("success"));
//                result.put("message", jsonObject.getString("message"));
//            }
//        }catch (Exception e){
//            result.put("success", false);
//            result.put("message", e.getMessage());
//        }
//        return result;
//    }
//
//    private List buildDataList(JSONArray dataArray) throws Exception{
//        Map<String,String> checkMap = new HashMap<>();
//        List result = new ArrayList();
//        for (Object data : dataArray){
//            JSONObject item = (JSONObject)JSONObject.toJSON(data);
//            log.info("buildDataList_item" + JSON.toJSONString(item));
//            log.info("buildDataList_checkMap" + JSON.toJSONString(checkMap));
//            item.put("title", item.getString("goodsName"));
//            item.put("quantity", item.getString("goodsNum"));
//            item.put("produceDate", item.getString("produceDate").replace(".","-"));
//            result.add(item);
//            if (!checkMap.isEmpty() && item.getString("produceDate").equals(checkMap.get(item.getString("scItemId")))){
//                throw new RuntimeException(item.getString("goodsName")+"_存在相同的生产日期记录_"+item.getString("produceDate"));
//            }
//            checkMap.put(item.getString("scItemId"), item.getString("produceDate"));
//        }
//        return result;
//    }
//}
